page.tsx 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use client";
  2. import { useEffect, useState } from "react";
  3. import { useRouter } from "next/navigation";
  4. import { useI18n } from "locales/client";
  5. import { useWorkoutSessionService } from "@/shared/lib/workout-session/use-workout-session.service";
  6. import { WorkoutSessionList } from "@/features/workout-session/ui/workout-session-list";
  7. import { WorkoutSessionHeatmap } from "@/features/workout-session/ui/workout-session-heatmap";
  8. import { Button } from "@/components/ui/button";
  9. import type { WorkoutSession } from "@/shared/lib/workout-session/types/workout-session";
  10. export default function ProfilePage() {
  11. const router = useRouter();
  12. const t = useI18n();
  13. const [sessions, setSessions] = useState<WorkoutSession[]>([]);
  14. const { getAll } = useWorkoutSessionService();
  15. useEffect(() => {
  16. const loadSessions = async () => {
  17. const loadedSessions = await getAll();
  18. setSessions(loadedSessions);
  19. };
  20. loadSessions();
  21. }, []);
  22. const values: Record<string, number> = {};
  23. sessions.forEach((session) => {
  24. const date = session.startedAt.slice(0, 10);
  25. values[date] = (values[date] || 0) + 1;
  26. });
  27. const until =
  28. sessions.length > 0
  29. ? sessions.reduce((max, s) => (s.startedAt > max ? s.startedAt : max), sessions[0].startedAt).slice(0, 10)
  30. : new Date().toISOString().slice(0, 10);
  31. return (
  32. <div>
  33. <WorkoutSessionHeatmap until={until} values={values} />
  34. <WorkoutSessionList />
  35. <div className="mt-8 flex justify-center">
  36. <Button onClick={() => router.push("/")} size="large">
  37. {t("profile.new_workout")}
  38. </Button>
  39. </div>
  40. </div>
  41. );
  42. }